package ee.greaton.siteManager.DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import ee.greaton.siteManager.utils.Config;

public class SQLConnection {

	private static SQLConnection instance = new SQLConnection();
	public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
	public static String URL = Config.db;
	public static String USER = Config.user;
	public static String PASSWORD = Config.pass;

	 //private constructor
    private SQLConnection() {
        try {
            //Step 2: Load MySQL Java driver
            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
     
    private Connection createConnection() {
 
        Connection connection = null;
        try {
            //Step 3: Establish Java MySQL connection
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            System.out.println("ERROR: Unable to Connect to Database.");
        }
        return connection;
    }   
    
    private Connection createConnection(String URL, String USER, String PASSWORD) {
    	
    	Connection connection = null;
    	try {
    		//Step 3: Establish Java MySQL connection
    		connection = DriverManager.getConnection(URL, USER, PASSWORD);
    	} catch (SQLException e) {
    		System.out.println("ERROR: Unable to Connect to Database.");
    	}
    	return connection;
    }   
     
    public static Connection getConnection() {
        return instance.createConnection();
    }
    
    public static Connection getConnection(String URL, String USER, String PASSWORD) {
    	return instance.createConnection(URL, USER, PASSWORD);
    }
}
